package com.xu.sso.db.service.impl;

import com.xu.sso.db.dao.base.ResourcesMapper;
import com.xu.sso.db.dao.base.ResourcesRoleMapper;
import com.xu.sso.db.model.base.Resources;
import com.xu.sso.db.model.base.ResourcesExample;
import com.xu.sso.db.model.base.ResourcesRole;
import com.xu.sso.db.model.base.ResourcesRoleExample;
import com.xu.sso.db.service.ResourcesDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResourcesDBServiceImpl implements ResourcesDBService {

    @Autowired
    private ResourcesMapper resourcesMapper;
    @Autowired
    private ResourcesRoleMapper resourcesRoleMapper;

    @Override
    public List<Resources> findAllResourcesByApp(String appId) {
        ResourcesExample example = new ResourcesExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return resourcesMapper.selectByExample(example);
    }

    @Override
    public List<ResourcesRole> findAllResourcesRole() {
        return resourcesRoleMapper.selectByExample(new ResourcesRoleExample());
    }

    @Override
    public int createResources(Resources resources) {
        resources.setCreateTime(new Date());
        resources.setUpdateTime(new Date());
        return resourcesMapper.insertSelective(resources);
    }

    @Override
    public Resources findResourcesById(Integer id) {
        return resourcesMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateResources(Resources resources) {
        resources.setUpdateTime(new Date());
        return resourcesMapper.updateByPrimaryKeySelective(resources);
    }

    @Override
    public int deleteResourcesById(Integer id) {
        return resourcesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteResourcesByApp(String appId) {
        ResourcesExample example = new ResourcesExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return resourcesMapper.deleteByExample(example);
    }
}
